package reflect;

import java.io.File;
import java.net.URISyntaxException;

/**
 * 实例化与当前类Test所在同一个包中的每个类的实例(使用无参构造器)
 */
public class Test {
    public static void main(String[] args) throws Exception {
        Class cls = Test.class;
        File dir = new File(
                cls.getResource(".").toURI()
        );
        String packageName = cls.getPackage().getName();
        File[] files = dir.listFiles(f->f.getName().endsWith(".class"));
        for(File file:files){
            String fileName = file.getName();
            String className = fileName.substring(0,fileName.indexOf("."));
            className = packageName+"."+className;

            Class c = Class.forName(className);
            Object o = c.newInstance();
            System.out.println(o);
        }
    }
}
